package algorithm;

import bean.Credit_parameter;
import java.util.HashMap;
import java.util.Map;
public class Dynamic_credit {
	public static final int praise = 0;
	public static final int review = 1;
	public static final int attention = 2;
	public static final int publish = 3;
	public static final int forword = 4;
	
	//被点赞，评论，关注人的积分加分规则
	public Map<String,Integer> Get_being_credit(int type,int number){   //type表示加分的类型,number表示一个动态里点赞（等）动作的历史数据
		Map<String,Integer> newmap = new HashMap<String,Integer>();     
		if(type == praise){                                             
			if(Get_being_praise(number) == 0){   //判定积分是否变化
				newmap.put("flag", 0);                                  //flag表示是否有积分变化的int值，0表示未变化，1表示有变化
				newmap.put("credit", Get_being_praise(number));
			}
			else{
				newmap.put("flag", 1);
				newmap.put("credit", Get_being_praise(number));
			}
		}
		else if(type == review){ 
			if(Get_being_review(number) == 0){
				newmap.put("flag", 0);
				newmap.put("credit", Get_being_review(number));
			}
			else{
				newmap.put("flag", 1);
				newmap.put("credit", Get_being_review(number));
			}
		}
		else if(type == attention){ 
			if(Get_being_attention(number) == 0){
				newmap.put("flag", 0);
				newmap.put("credit", Get_being_attention(number));
			}
			else{
				newmap.put("flag", 1);
				newmap.put("credit", Get_being_attention(number));
			}
		}
		else{
			System.out.println("PARAMETER ERROR !!");
		}
		return newmap;
	}
	
	//点赞，评论，关注人的积分加分规则
	public Map<String,Integer> Get_credit(int type){      //type表示加分的类型
		Map<String,Integer> newmap = new HashMap<String,Integer>(); 
		if(type == publish){
			newmap.put("flag",1);
			newmap.put("credit",Get_dynamic());
		}
		else if(type == praise){
			newmap.put("flag",1);
			newmap.put("credit",Get_praise());
		}
		else if(type == review){
			newmap.put("flag",1);
			newmap.put("credit",Get_review());
		}
		else if(type == attention){
			newmap.put("flag",1);
			newmap.put("credit",Get_attention());
		}
		return newmap;
	}
	
	//发布动态规则
	private int Get_dynamic(){
		return Credit_parameter.getPublish_dynamic();
	}
	//点赞加分规则
	private int Get_praise(){
		return Credit_parameter.getPraise();
	}
	//评论加分规则
	private int Get_review(){
		return Credit_parameter.getReview();
	}
	//关注加分规则
	private int Get_attention(){
		return Credit_parameter.getAttention();
	}

	//被点赞加分规则
	private int Get_being_praise(int credit){
		double temp = Math.floor( Math.log(credit+1) / Math.log(Credit_parameter.getPraise_log()) ) - 
				Math.floor( Math.log(credit) / Math.log(Credit_parameter.getPraise_log()) );
		if(credit+1 < Credit_parameter.getPraise_log()){  //如果点赞数比log底数还要小，返回0
			credit = 0;
		} 
		else if(temp == 0){    //如果点赞数+1后没有变化没满1，返回0
			credit = 0; 
		}
		else if(temp == 1 ){   //变化满1，返回1
			credit = 1;
		}
		else{                 //错误情况
			credit = 0;
		}
		return credit;
	}
	
	//被评论加分规则
	private int Get_being_review(int credit){
		double temp = Math.floor( Math.log(credit+1) / Math.log(Credit_parameter.getReview_log()) ) - 
				Math.floor( Math.log(credit) / Math.log(Credit_parameter.getReview_log()) );
		if(credit+1 < Credit_parameter.getReview_log()){
			credit = 0;
		}
		else if(temp == 0){
			credit = 0; 
		}
		else if(temp == 1){
			credit = 1;
		}
		else{
			credit = 0;
		}
		return credit;
	}
	
	//被关注加分规则
	private int Get_being_attention(int credit){
		double temp = Math.floor( Math.log(credit+1) / Math.log(Credit_parameter.getAttention_log()) ) - 
				Math.floor( Math.log(credit) / Math.log(Credit_parameter.getAttention_log()) );
		if(credit+1 < Credit_parameter.getAttention_log()){
			credit = 0;
		}
		else if(temp == 0){
			credit = 0; 
		}
		else if(temp == 1){
			credit = 1;
		}
		else{
			credit = 0;
		}
		return credit;
	}
	
}
